#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>   
#include <unistd.h>
#include "memalloc.h"

#define NUM 20000
#define SIZE 5240

// g++ -std=c++11 test.c memalloc_adapter.cpp

int main() {
	size_t* id = (size_t*)malloc(sizeof(size_t) * NUM);
	int *x, i;
	int **y = (int**)malloc(sizeof(int*) * NUM);
	float time = 0;
	clock_t beg, end;
	
	beg = clock(); 
	if (!ma_init(2000000000, 20000000000, "c")) {
		perror("CRASH");
		return 0;
	}
	end = clock();
	time = (end - beg) / (CLOCKS_PER_SEC / 1000);
	printf("for ma_init: %f\n", time);


	beg = clock(); 
	for (i = 0; i < NUM; ++i) {
		id[i] = ma_alloc(SIZE * (i % 10));
		//usleep(5);
		if (id[i])
			x = (int *)ma_get(id[i]);
		else
			return -1;
		if (i % 1000 == 0)
			printf("<%8d>\n", i);
	}
	end = clock();
	time = (end - beg) / (CLOCKS_PER_SEC / 1000);
	printf("for ma_alloc_get: %f\n", time);
	
	
	beg = clock(); 
	for (i = 0; i < NUM; ++i) {
		if (id[i])
			ma_release(id[i]);
	}
	end = clock();
	time = (end - beg) / (CLOCKS_PER_SEC / 1000);
	printf("for ma_release: %f\n", time);
	
	
	beg = clock(); 
	for (i = 0; i < NUM; ++i) {
		if (id[i])
			ma_free(id[i]);
	}
	end = clock();
	time = (end - beg) / (CLOCKS_PER_SEC / 1000);
	printf("for ma_free_get: %f\n", time);


	beg = clock(); 
	for (i = 0; i < NUM; ++i) {
		id[i] = ma_alloc(SIZE * (i % 10));
	}
	end = clock();
	time = (end - beg) / (CLOCKS_PER_SEC / 1000);
	printf("for ma_alloc: %f\n", time);
	
	
	
	beg = clock(); 
	for (i = 0; i < NUM; ++i) {
		if (id[i])
			ma_free(id[i]);
	}
	end = clock();
	time = (end - beg) / (CLOCKS_PER_SEC / 1000);
	printf("for ma_free: %f\n", time);


	beg = clock(); 
	for (i = 0; i < NUM; ++i) {
		y[i] = (int *)malloc(SIZE);
	}
	end = clock();
	time = (end - beg) / (CLOCKS_PER_SEC / 1000);
	printf("for malloc: %f\n", time);



	beg = clock(); 
	for (i = 0; i < NUM; ++i) {
		free(y[i]);
	}
	end = clock();
	time = (end - beg) / (CLOCKS_PER_SEC / 1000);
	printf("for free: %f\n\n", time);
		

	ma_deinit();
	return 0;
}
